home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / jabber / JabberConversation.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  6KB  |  170 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from jabber.objects.x_event import X_Event, X_EVENT_NS
  5. from jabber.objects.chatstates import ChatState, CHATSTATES_NS
  6. from common.Conversation import Conversation
  7. from pyxmpp.message import Message
  8. from logging import getLogger
  9. from jabber import JabberBuddy
  10. from pyxmpp.xmlextra import common_ns
  11. from util import format_xhtml, callsback
  12. from pyxmpp.utils import from_utf8
  13. from jabber.objects.x_delay import X_DELAY_NS, X_Delay
  14. import libxml2
  15. log = getLogger('jabber.JabberConversation')
  16. XHTML_IM_NS = 'http://jabber.org/protocol/xhtml-im'
  17. XHTML_NS = 'http://www.w3.org/1999/xhtml'
  18. xdata_namespaces = {
  19.     'xhtmlim': XHTML_IM_NS,
  20.     'xhtml': XHTML_NS }
  21. typing_chatstates = dict(typing = 'composing', typed = 'paused')
  22.  
  23. class JabberConversation(Conversation):
  24.     
  25.     def __init__(self, protocol, buddy, jid_to, thread = None):
  26.         if not isinstance(buddy, JabberBuddy.JabberBuddy):
  27.             raise TypeError
  28.         
  29.         Conversation.__init__(self)
  30.         self.protocol = protocol
  31.         self.buddy_to = buddy
  32.         self.jid_to = jid_to
  33.         self.buddies = protocol.buddies
  34.         self.thread = thread
  35.         self.name = buddy.alias
  36.  
  37.     ischat = False
  38.     
  39.     def self_buddy(self):
  40.         return self.protocol.self_buddy
  41.  
  42.     self_buddy = property(self_buddy)
  43.     
  44.     def buddy(self):
  45.         return self.buddy_to
  46.  
  47.     buddy = property(buddy)
  48.     
  49.     def _send_message(self, message, auto = False, format = None, callback = None, **opts):
  50.         m = Message(stanza_type = 'chat', to_jid = self.jid_to, body = message)
  51.         message = unicode(message.encode('xml'))
  52.         if format is not None:
  53.             append_formatted_html(m, message, format)
  54.         
  55.         ChatState('active').as_xml(m.xmlnode)
  56.         X_Event(composing = True).as_xml(m.xmlnode)
  57.         
  58.         try:
  59.             self.protocol.send_message(m)
  60.         except Exception:
  61.             e = None
  62.             callback.error(e)
  63.  
  64.         callback.success()
  65.  
  66.     _send_message = callsback(_send_message)
  67.     
  68.     def send_typing_status(self, status):
  69.         m = Message(to_jid = self.jid_to, stanza_type = 'chat')
  70.         node = m.xmlnode
  71.         X_Event(composing = status == 'typing').as_xml(node)
  72.         ChatState(typing_chatstates.get(status, 'active')).as_xml(node)
  73.         self.protocol.send_message(m)
  74.  
  75.     
  76.     def buddy_join(self, buddy):
  77.         self.room_list.append(buddy)
  78.         self.typing_status[buddy] = None
  79.  
  80.     
  81.     def incoming_message(self, buddy, message):
  82.         self.jid_to = message.get_from()
  83.         body = get_message_body(message)
  84.         if body:
  85.             stamp = get_message_timestamp(message)
  86.             if stamp:
  87.                 self.received_message(buddy, body, timestamp = stamp, offline = True)
  88.             else:
  89.                 self.received_message(buddy, body)
  90.             Conversation.incoming_message(self)
  91.         
  92.         self.typing_status[buddy] = get_message_chatstate(message, body)
  93.  
  94.     
  95.     def id(self):
  96.         return (self.buddy_to,)
  97.  
  98.     id = property(id)
  99.     
  100.     def exit(self):
  101.         self.protocol.conversations.pop(self.id, None)
  102.  
  103.  
  104.  
  105. def append_formatted_html(message_tag, message, format):
  106.     span_text = format_xhtml(message, format)
  107.     html = message_tag.xmlnode.newChild(None, 'html', None)
  108.     xhtml_ns = html.newNs(XHTML_IM_NS, None)
  109.     body_text = '<body xmlns="%s">%s</body>' % (XHTML_NS, span_text)
  110.     
  111.     try:
  112.         message_doc = libxml2.parseDoc(body_text.encode('utf-8'))
  113.     except Exception:
  114.         import traceback as traceback
  115.         traceback.print_exc()
  116.         print 'This text failed: %r' % body_text
  117.         raise 
  118.  
  119.     message_node = message_doc.get_children()
  120.     message_node_copy = message_node.docCopyNode(message_tag.xmlnode.doc, 1)
  121.     html.addChild(message_node_copy)
  122.     message_doc.freeDoc()
  123.     return span_text
  124.  
  125.  
  126. def get_message_timestamp(message):
  127.     xdelays = message.xpath_eval(u'jxd:x', {
  128.         'jxd': X_DELAY_NS })
  129.     if xdelays:
  130.         delay = X_Delay(xdelays[0])
  131.         if delay.timestamp is not None:
  132.             return delay.timestamp
  133.         
  134.     
  135.  
  136.  
  137. def get_message_chatstate(message, body):
  138.     xevents = message.xpath_eval(u'jxe:x', {
  139.         'jxe': X_EVENT_NS })
  140.     chatstates = message.xpath_eval('cs:*', {
  141.         'cs': CHATSTATES_NS })
  142.     if chatstates:
  143.         chatstate = ChatState(chatstates[0]).xml_element_name
  144.         if chatstate == 'composing':
  145.             return 'typing'
  146.         elif chatstate == 'paused':
  147.             return 'typed'
  148.         else:
  149.             log.warning('got an unknown chatstate: %s', chatstate)
  150.             return None
  151.     elif xevents:
  152.         return None if X_Event(xevents[0]).composing and not body else None
  153.     
  154.  
  155.  
  156. def get_message_body(message):
  157.     jid = message.get_from()
  158.     xdata = message.xpath_eval(u'xhtmlim:html/xhtml:body[1]/node()', xdata_namespaces)
  159.     if xdata:
  160.         body = from_utf8(''.join((lambda .0: for child in .0:
  161. child.serialize())(xdata)))
  162.     else:
  163.         body = message.get_body()
  164.         body = None if body else None
  165.         if body is not None:
  166.             body = body.replace('\n', '<br />')
  167.         
  168.     return body
  169.  
  170.